﻿/**
 * Peter
 * Created by: Peter Development Team
 *    http://peter.codeplex.com/
 * 
 * GNU General Public License version 2 (GPLv2)
 *    http://peter.codeplex.com/license
 *
 *  This code is provided on an AS IS basis, with no WARRANTIES,
 *  CONDITIONS or GUARANTEES of any kind.
 *  
 **/

using System.Windows.Input;
using Peter.Common;

namespace Peter.Logic.Dialogs
{
   /// <summary>
   /// Model for a new project.
   /// </summary>
   public class ModelNewProject : ModelProjectBase
   {
      private string m_NewProjectName;
      private string m_NewProjectLocation;
      private RelayCommand m_CmdBrowse;
      private RelayCommand m_CmdHideBrowse;

      /// <summary>
      /// Initializes the model for a new project.
      /// </summary>
      /// <param name="projectManager">The project manager.</param>
      public ModelNewProject (ModelProjectManager projectManager)
         : base (projectManager)
      {
         this.Width = 350;
         this.Height = 300;
         this.OkButtonText = "Create";
      }

      /// <summary>
      /// Gets or Sets the name of the new project.
      /// </summary>
      public string NewProjectName
      {
         get { return this.m_NewProjectName; }
         set
         {
            this.m_NewProjectName = value;
            this.RaisePropertyChanged ("NewProjectName");
         }
      }

      /// <summary>
      /// Gets or Sets the location of the new project.
      /// </summary>
      public string NewProjectLocation
      {
         get { return this.m_NewProjectLocation; }
         set
         {
            this.m_NewProjectLocation = value;
            this.RaisePropertyChanged ("NewProjectLocation");
         }
      }

      /// <summary>
      /// Gets the browse command.
      /// </summary>
      public ICommand BrowseCommand
      {
         get { return this.m_CmdBrowse ?? (this.m_CmdBrowse = new RelayCommand (this.OnBrowse)); }
      }

      /// <summary>
      /// Gets the hide browse command.
      /// </summary>
      public ICommand HideBrowseCommand
      {
         get { return this.m_CmdHideBrowse ?? (this.m_CmdHideBrowse = new RelayCommand (this.OnHideBrowse)); }
      }

      /// <summary>
      /// Command used to browse folders.
      /// </summary>
      /// <param name="obj">Command parameter.</param>
      private void OnBrowse (object obj)
      {
         this.Width = 670;
      }

      private void OnHideBrowse (object obj)
      {
         this.Width = 350;
      }

      /// <summary>
      /// Occurs when the selected location changes.
      /// </summary>
      /// <param name="location">The new location.</param>
      protected override void SelectedLocationChanged (string location)
      {
         this.NewProjectLocation = location;
      }

      /// <summary>
      /// Checks if the ok command can execute.
      /// </summary>
      /// <returns>If it is ok to execute.</returns>
      public override bool CanOkExecute ()
      {
         return this.SelectedItem != null &&
                !string.IsNullOrEmpty (this.NewProjectName) &&
                !string.IsNullOrEmpty (this.NewProjectLocation);
      }

      /// <summary>
      /// Occurs when the ok button on the dialog is pressed.
      /// </summary>
      public override void OkExecuted ()
      {
         this.SelectedItem.Create (this.NewProjectName, this.NewProjectLocation);
         base.OkExecuted ();
      }
   }
}
